package br.nextfm.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.nextfm.dao.UsuarioDao;
import br.nextfm.model.Administrador;

public class AutenticarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String login = request.getParameter("login");
		String senha = request.getParameter("senha");
		String pin = request.getParameter("pin");
		
		UsuarioDao usuarioDao = new UsuarioDao();
		
		Administrador administrador = usuarioDao.getAdministradorByLogin(login, senha, pin);
		HttpSession session = request.getSession();
		
		if (administrador != null) {

			session.setAttribute("administrador", administrador);
			response.sendRedirect("home.jsp");
			
		}
		
		else {
			
			request.setAttribute("erroLogin", true);
			request.getRequestDispatcher("index.jsp").forward(request, response);
						
		}
				
	}

}
